package cc.forgemod.customicon.client.renderer;

import cc.forgemod.customicon.common.icon.Icon;
import cc.forgemod.customicon.common.icon.IconFlag;
import cc.forgemod.customicon.common.icon.IconManager;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.event.RenderTooltipEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class RenderListener extends ARenderListener {

    public static final RenderListener INSTANCE = new RenderListener();

    @SubscribeEvent
    public void onPreRenderToolTip(RenderTooltipEvent.Pre pEvent) {
        Icon tIcon = IconManager.getIcon(pEvent.getStack());
        if (tIcon != null && tIcon.hasFlag(IconFlag.HIDDEN_TOOLTIP)) {
            pEvent.setCanceled(true);
        }
    }

    // PlayerControllerMP.drawScreen  op=replace(slotClick)
    public static ItemStack slotClick(Container pGui, int pSlotId, int pCtrlKeyDown, ClickType pClickType, EntityPlayer pPlayer) {
        if (clickCheck(getSlot(pGui, pSlotId), IconFlag.FIXED, pCtrlKeyDown, pClickType == ClickType.SWAP))
            return pGui.slotClick(pSlotId, pCtrlKeyDown, pClickType, pPlayer);

        return null;
    }

    //GuiContainer.handleMouseClick  --> op=interrupt
    public static boolean preHandleMouseClick(Slot pSlot, int pSlotId, int pMouseButton, ClickType pType) {
        return clickCheck(pSlot, IconFlag.NOCLICK, pMouseButton, pType == ClickType.SWAP);
    }

    public static boolean isMouseOverSlot(GuiContainer pGui, Slot pSlot, int pMouseX, int pMouseY) {
        return isMouseOverSlot0(pGui, pSlot, pMouseX, pMouseY);
    }

    //Minecraft.displayGuiScreen  op=Insert(head)
    public static void preDisplayGuiScreen(GuiScreen pScreen) {
        preDisplayGuiScreen0(pScreen);
    }

    public static IBakedModel postGetItemModel(IBakedModel pModel, ItemStack pStack) {
        Icon tIcon = IconManager.getIcon(pStack);
        if (tIcon != null && tIcon.initSuccess()) {
            return tIcon.getTexture().getBakedModel();
        }

        return pModel;
    }
}
